package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import io.swagger.annotations.ApiOperation;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.dto.ElevatorPrincipalQueryDTO;
import org.springblade.modules.admin.dto.ElevatorPrincipalSaveDTO;
import org.springblade.modules.admin.entity.ElevatorPrincipalUser;
import org.springblade.modules.admin.service.ElevatorPrincipalDeptService;
import org.springblade.modules.admin.service.ElevatorPrincipalUserService;
import org.springblade.modules.admin.service.ElevatorService;
import org.springblade.modules.admin.vo.ElevatorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Date 9:34 2023/2/9
 * @Description {
 * 电梯责任组人
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/elevator/principal")
public class ElevatorPrincipalController {

	@Autowired
	private ElevatorPrincipalDeptService principalDeptService;
	@Autowired
	private ElevatorPrincipalUserService principalUserService;
	@Autowired
	private ElevatorService elevatorService;

	/**
	 * @return 成功与否
	 * @Author lsay
	 * @Description {
	 * 电梯责任组新增或修改
	 * }
	 * @Date 9:40 2023/2/9
	 * @Param 电梯责任组新增参数
	 **/
	@PostMapping("/dept/save")
	public R<Boolean> savePrincipalDept(@RequestBody ElevatorPrincipalSaveDTO saveDTO) {
		return R.data(principalDeptService.savePrincipalDept(saveDTO));
	}

	/**
	 * @return 成功与否
	 * @Author lsay
	 * @Description {
	 * 电梯责任人新增或修改
	 * }
	 * @Date 9:40 2023/2/9
	 * @Param 电梯责任人新增参数
	 **/
	@PostMapping("/user/save")
	public R<Boolean> savePrincipalUser(@RequestBody ElevatorPrincipalSaveDTO saveDTO) {
		return R.data(principalUserService.savePrincipalUser(saveDTO));
	}

	/**
	 * @return
	 * @Author lsay
	 * @Description {
	 * Excel 导出电梯责任管理信息
	 * }
	 * @Date 16:05 2023/3/29
	 * @Param
	 **/
	@PostMapping("/down/excel")
	public void downExcelDetail(HttpServletResponse response, @RequestBody ElevatorPrincipalQueryDTO queryDTO) throws IOException {
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("项目名称", "项目地址", "梯号", "年审类型", "保养状态", "电梯合同保障状态","年审梯保养合规状态", "违规天数", "上次维保签字日期", "下次保养计划时间", "电梯所属维保组", "维保负责人", "最后更新时间", "是否外包", "电梯唯一识别码");
			rows.add(title);
			elevatorService.downExcelElevatorPrincipal(queryDTO, rows);
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			CommonUtil.setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}

	}


	/**
	 * 到处数据
	 *
	 * @param response
	 * @param queryDTO
	 * @throws IOException
	 */
	@PostMapping("/down/excel/v2")
	public void downExcelDetail_v2(HttpServletResponse response, @RequestBody ElevatorPrincipalQueryDTO queryDTO) throws IOException {
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("项目名称", "项目地址", "梯号", "年审类型", "维保电梯状态","维保状态细分","电梯合同保障状态","年审梯保养合规状态", "违规天数", "上次维保签字日期", "下次保养计划时间", "电梯所属维保组", "维保负责人", "最后更新时间", "是否外包", "电梯唯一识别码",
					"电梯项目类型", "电梯品牌", "设备注册代码", "合同编号","审核状态", "合同来源", "合同类型", "合同属性", "使用单位", "客户单位", "客户电话", "是否为外包合同", "维保单位", "签约人", "合同开始",
					"合同结束", "是否驻场", "年检费用合计", "限速器年检费", "月保养费用", "服务月数", "付款周期", "合同总额", "支付方式", "票据类型"
				);
			rows.add(title);
			elevatorService.downExcelElevatorPrincipal_v2(queryDTO, rows);
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			CommonUtil.setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}

	}

}
